package com.czh.mock.providers.text.datetime;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class DatetimeConfig {
    /**
     * 格式
     */
    private DatetimeType type;
    private String format = "yyyy-MM-dd HH:mm:ss";
    private Long minTimestamp;
    private Long maxTimestamp;
    public static final long MIN_TIMESTAMP = 0L;
    public static final long MAX_TIMESTAMP = 253402300799L;
    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static DatetimeConfig build() {
        return new DatetimeConfig(
                DatetimeType.TIMESTAMP, null, null, null
        );
    }

    public static DatetimeConfig build(DatetimeType type, String format, long minTimestamp, long maxTimestamp) {
        return new DatetimeConfig(type, format, minTimestamp, maxTimestamp);
    }

    public static DatetimeConfig build(DatetimeType type, String format, String minTime, String maxTime) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern(DEFAULT_DATETIME_FORMAT);
        LocalDateTime minDatetime = LocalDateTime.parse(minTime, pattern);
        LocalDateTime maxDatetime = LocalDateTime.parse(maxTime, pattern);
        return new DatetimeConfig(
                type, format,
                minDatetime.toEpochSecond(ZoneOffset.of("+8")),
                maxDatetime.toEpochSecond(ZoneOffset.of("+8"))
        );
    }

    private DatetimeConfig(DatetimeType type, String format, Long minTimestamp, Long maxTimestamp) {
        this.type = type;
        if (format != null) {
            this.format = format;
        }
        this.minTimestamp = minTimestamp;
        this.maxTimestamp = maxTimestamp;
    }

    public DatetimeType getType() {
        return type;
    }

    public String getFormat() {
        return format;
    }

    public Long getMinTimestamp() {
        return minTimestamp;
    }

    public Long getMaxTimestamp() {
        return maxTimestamp;
    }

    public enum DatetimeType {
        TIMESTAMP,
        DATETIME
    }
}
